package controllers

import (
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"staff/cache"
	"staff/models"
	"staff/pkg"
	"strconv"
	"time"
)

type StaffController struct {
	beego.Controller
}

type Res struct {
	Code int         `json:"code"`
	Res  string      `json:"res"`
	Data interface{} `json:"data"`
}

func (c *StaffController) Login() {
	username := c.GetString("username")
	password := c.GetString("password")
	if username == "" || password == "" {
		c.Data["json"] = Res{
			Code: 0,
			Res:  "参数错误",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	var user models.User
	err := models.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Res:  "用户不存在",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	key := "novel" + user.Username
	errData, _ := cache.Rdb.Get(cache.Ctx, key).Int()
	if errData >= 3 {
		c.Data["json"] = Res{
			Code: 0,
			Res:  "登录超过三次",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, key)
		if errData == 2 {
			cache.Rdb.Expire(cache.Ctx, key, time.Hour*2)
		}
		c.Data["json"] = Res{
			Code: 0,
			Res:  "登录失败封禁两个小时",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code: 200,
		Res:  "登录成功",
		Data: user,
	}
	c.ServeJSON()
}

func (c *StaffController) List() {
	var staff []models.StaEn
	err := models.DB.Find(&staff).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Res:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code: 200,
		Res:  "查询成功",
		Data: staff,
	}
	c.ServeJSON()
}

func (c *StaffController) Add() {
	username := c.GetString("username")
	password := c.GetString("password")
	user := models.User{
		Username: username,
		Password: password,
	}
	err := models.DB.Create(&user).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Res:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code: 200,
		Res:  "添加成功",
		Data: nil,
	}
	c.ServeJSON()
}

func (c *StaffController) UserList() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	var user []models.User
	err := models.DB.Find(&user).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Res:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code: 200,
		Res:  "查询成功",
		Data: user,
	}
	c.ServeJSON()
}

func (c *StaffController) UserAdd() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	username := c.GetString("username")
	phone := c.GetString("phone")
	password := c.GetString("password")
	dage := c.GetString("dage")
	user := models.User{
		Username: username,
		Phone:    phone,
		Password: password,
		Dage:     dage,
	}
	err := models.DB.Create(&user).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Res:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code: 200,
		Res:  "添加成功",
		Data: nil,
	}
	c.ServeJSON()
}

func (c *StaffController) Limit() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	i := (page - 1) * size
	var stabos models.StaBos
	key := "novel"
	res, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if res == "" {
		fmt.Println("从mysql中获取数据")
		models.DB.Find(&stabos)

		jsonData, _ := json.Marshal(&stabos)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*24)
	} else {
		fmt.Println("从redis中获取数据")
		json.Unmarshal([]byte(res), key)
	}
	err := models.DB.Offset(i).Limit(size).First(&stabos).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Res:  "查询分页失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code: 200,
		Res:  "查询分页成功",
		Data: stabos,
	}
	c.ServeJSON()
}

func (c *StaffController) Del() {
	id, _ := c.GetInt("id")
	err := models.DB.Where("id=?", id).Delete(&models.StaBos{}).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Res:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code: 200,
		Res:  "删除成功",
		Data: nil,
	}
	c.ServeJSON()
}

func (c *StaffController) Upload() {
	var stabos models.StaBos
	id, _ := c.GetInt("id")
	key := "novel" + strconv.Itoa(id)
	res, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if res == "" {
		fmt.Println("从mysql中获取数据")
		models.DB.Find(&stabos)

		jsonData, _ := json.Marshal(&stabos)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*24)
	} else {
		fmt.Println("从redis中获取数据")
		json.Unmarshal([]byte(res), key)
	}
	err := models.DB.Where("id=?", id).First(&stabos).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Res:  "查询详情失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code: 200,
		Res:  "查询详情成功",
		Data: stabos,
	}
	c.ServeJSON()
}
